/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */
package com.wttech.dzzf.process.service.errordata;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wttech.dzzf.process.dao.ProcessUserInfoDao;
import com.wttech.dzzf.process.model.ErrorInfo;
import com.wttech.dzzf.process.util.CertVerificationService.CertifiTypeCheck;
import com.wttech.dzzf.process.util.CertVerificationService.CreditCodeCheck;
import com.wttech.dzzf.process.util.CertVerificationService.IdCardCheck;
import com.wttech.dzzf.process.util.CertVerificationService.OrgCertCheck;

/**
 * 纠正错误的证件信息
 * 
 * @date 2017年9月25日
 * @author MaShibo
 */
@Service
public class CorrectErrorUserInfoService {
    
    
    private static final Logger LOGGER = LoggerFactory.getLogger(CorrectErrorUserInfoService.class);
    
    @Autowired
    private ProcessUserInfoDao processUserInfoDao;
    
    /**
     * 身份证检测
     */
    private IdCardCheck idCardCheck = new IdCardCheck();
    
    /**
     * 组织机构代码证检测
     */
    private OrgCertCheck orgCertCheck = new OrgCertCheck();
    
    /**
     * 社会统一信用大妈证检测
     */
    private CreditCodeCheck creditCodeCheck = new CreditCodeCheck();
    
    /**
     * 证件类型检测
     */
    private CertifiTypeCheck certifiTypeCheck = new CertifiTypeCheck();
    
    public void process() {
        ErrorInfo info = new ErrorInfo();
        info.setState(0);
        @SuppressWarnings("unchecked")
        List<ErrorInfo> errorInfos = (List<ErrorInfo>) processUserInfoDao.queryList(info);
        LOGGER.info("共{}条待处理数据", errorInfos.size());
        int i = 0;
        for (ErrorInfo errorInfo : errorInfos) {
            Integer certifiType = errorInfo.getCertifiType();
            String certifiNo = errorInfo.getCertifiNo();
            
            if (orgCertCheck.test(certifiNo)) {
                errorInfo.setCertifiType(101);
                errorInfo.setState(1);
                processUserInfoDao.updateObject(errorInfo);
                i++;
            } else if ((certifiType.equals(101) || !certifiTypeCheck.test(certifiType))
                    && creditCodeCheck.test(certifiNo)) {
                errorInfo.setCertifiType(102);
                errorInfo.setState(1);
                processUserInfoDao.updateObject(errorInfo);
                i++;
            } else if (certifiType.equals(1) && idCardCheck.test(errorInfo.getCertifiNo())) {
                errorInfo.setCertifiType(0);
                errorInfo.setState(1);
                processUserInfoDao.updateObject(errorInfo);
                i++;
            }
        }
        LOGGER.info("共处理{}条数据", i);
    }
    
}
